package com.jeesite.modules.storage.util;

import com.jeesite.common.config.Global;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2020-02-19 21:49
 **/
public class QCloudUtil {
    private static String appId = Global.getConfig("storage.qcloud.appId");
    private static String appKey = Global.getConfig("storage.qcloud.appKey");
    private static String secretKey = Global.getConfig("storage.qcloud.secretKey");
    private static String bucketName = "dacangku-1258820917";

    public static Boolean push(InputStream input){
        /*COSCredentials cred = new BasicCOSCredentials(appKey, secretKey);
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("COS_REGION");
        ClientConfig clientConfig = new ClientConfig(region);
// 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
// 方法1 本地文件上传
        File localFile = new File(localFilePath);
        String key = "exampleobject";
        PutObjectResult putObjectResult = cosClient.putObject(bucketName, key, input);
        String etag = putObjectResult.getETag();  // 获取文件的 etag

// 方法2 从输入流上传(需提前告知输入流的长度, 否则可能导致 oom)
        FileInputStream fileInputStream = new FileInputStream(localFile);
        ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置输入流长度为500
        objectMetadata.setContentLength(500);
// 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType("application/pdf");
        putObjectResult = cosClient.putObject(bucketName, key, fileInputStream, objectMetadata);
        etag = putObjectResult.getETag();
// 关闭输入流...

// 方法3 提供更多细粒度的控制, 常用的设置如下
// 1 storage-class 存储类型, 枚举值：Standard，Standard_IA，Archive。默认值：Standard
// 2 content-type, 对于本地文件上传，默认根据本地文件的后缀进行映射，例如 jpg 文件映射 为image/jpeg
//   对于流式上传 默认是 application/octet-stream
// 3 上传的同时指定权限(也可通过调用 API set object acl 来设置)
// 4 若要全局关闭上传MD5校验, 则设置系统环境变量，此设置会对所有的会影响所有的上传校验。 默认是进行校验的。
// 关闭MD5校验：  System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");
// 打开MD5校验  System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, null);
        localFile = new File(localFilePath);
        key = "picture.jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
// 设置存储类型为低频
        putObjectRequest.setStorageClass(StorageClass.Standard_IA);
// 设置自定义属性(如 content-type, content-disposition 等)
        objectMetadata = new ObjectMetadata();
// 设置 Content type, 默认是 application/octet-stream
        objectMetadata.setContentType("image/jpeg");
        putObjectRequest.setMetadata(objectMetadata);
        putObjectResult = cosClient.putObject(putObjectRequest);
        etag = putObjectResult.getETag();  // 获取文件的 etag*/
        return true;
    }
}
